package com.qd.videorecorder.adapter;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.qd.videorecorder.R;
import com.qd.videorecorder.SingAndSMusic;
import com.qd.videorecorder.SingLocalMusic;

public class LocalMp3Adapter extends BaseAdapter {

	private Context context;
	private File[] files;
	
	public LocalMp3Adapter(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
	}
	
	public void setData(File[] files) {
		// TODO Auto-generated method stub
		this.files = files;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return files.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		HolderView holderView = null;
		
		if(convertView==null){
			
			convertView = LayoutInflater.from(context).inflate(R.layout.localmp3, null);
			
			holderView = new HolderView();
			
			holderView.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
			holderView.tv_music_name = (TextView) convertView.findViewById(R.id.tv_music_name);
			holderView.btn_music_sing = (Button) convertView.findViewById(R.id.btn_music_sing);
			
			
			convertView.setTag(holderView);
		}else{
			holderView = (HolderView)convertView.getTag();
		}
		
		String strPos = "";
		
		if(position<9){
			strPos = "0"+(position+1);
		}else{
			strPos = ""+(position+1);
		}
		
		final String filename = files[position].getName();
		final String filepath = files[position].getAbsolutePath();
		final String pcmPath = filepath.substring(0, filepath.lastIndexOf("/")+1)+"pcm/"+filename.replace(".mp3", ".pcm"); 
		
		System.out.println(pcmPath);
		
		holderView.tv_number.setText(strPos);
		holderView.tv_music_name.setText(filename);
		holderView.btn_music_sing.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				File file = new File(pcmPath);
				if(file.exists()){
					Intent intent = new Intent();
					intent.setClass(context, SingAndSMusic.class);
					intent.putExtra("title", "伴奏");
					intent.putExtra("tag", "bz");
					intent.putExtra("filepath", filepath);
					intent.putExtra("filename", filename);
					intent.putExtra("pcmPath", pcmPath);
					context.startActivity(intent);
					((SingLocalMusic)context).finish();
				}else{
					
					((SingLocalMusic)context).decode();
					new Thread(){

						@Override
						public void run() {
							// TODO Auto-generated method stub
							super.run();
							int decode = ((SingLocalMusic)context).decodeMp3ToPCM2(filepath, pcmPath);
							if (decode==0) {
								
								((SingLocalMusic)context).decodeFinish();
								
								Intent intent = new Intent();
								intent.setClass(context, SingAndSMusic.class);
								intent.putExtra("title", "伴奏");
								intent.putExtra("tag", "bz");
								intent.putExtra("filepath", filepath);
								intent.putExtra("pcmPath", pcmPath);
								intent.putExtra("decode", decode);
								context.startActivity(intent);
								((SingLocalMusic)context).finish();
							}
						}
						
					}.start();
					
				}
				
			}
		});
		
		return convertView;
	}
	
	static class HolderView {

		public Button btn_music_sing;
		public TextView tv_number;
		public TextView tv_music_name;
		
	}

}
